home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++,comp.lang.c
- Path: uu4news.netcom.com!friend!news
- From: rich@kastle.com (Richard Krehbiel)
- Subject: Re: Include C++ in C programs
- Message-ID: <1996Mar25.141130.1485@friend.kastle.com>
- Sender: news@friend.kastle.com (News)
- Reply-To: rich@kastle.com
- Organization: Kastle Development Associates
- X-Newsreader: Forte Free Agent 1.0.82
- References: <1996Mar20.104314.28950@hgl.signaal.nl>
- Date: Mon, 25 Mar 1996 14:10:09 GMT
-
- glandrup@hgl.signaal.nl (Glandrup M.H.J.) wrote:
-
- >===============================================================================
- >Restriction: Unclassified
- >===============================================================================
-
- >Sorry about the header, but it is policy here...
-
-
- >Hello,
-
- >Is there anybody who knows how to call C++ member functions in a
- >C program?
-
- C++ member functions can only be called by C++ code.
-
- In C++ it's possible to declare two functions which have the same
- "name" but either take different arguments, or are members of
- different classes (or both), and yet the compiler can distinguish
- them. It does so by changing the function name ("mangling" it) so
- that it "somehow" includes the class name and argument types.
- Unfortunately, the "somehow" part is totally up to the compiler, and
- it may result in external names that are not representable in the
- character set allowed C symbol names.
-
- What you can do is create some C++ access functions with C-callable
- names.
-
- For an example, suppose you have:
-
- class a_class
- {
- void a_member(int);
- };
-
- You can create a C-callable access function like this:
-
- extern "C" void a_class_a_member(void *a_this, int i)
- {
- a_class *a = (a_class *)a_this; // caller must supply "this"
- a->a_member(i);
- }
-
- Now your C program can call a_class_a_member(). It's up to you to
- figure out how to get a proper "this" pointer.
-
- --
- Richard Krehbiel, Kastle Systems, Arlington VA USA
- rich@kastle.com (work) or richk@mnsinc.com (personal)
-
-